package com.itcat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itcat.mysql.pojo.UmsResource;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import javax.management.relation.Relation;
import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/3/31 19:54]
 */
@Mapper
public interface UmsAdminRoleRelationMapper extends BaseMapper<UmsResource> {

    @Select("<script>" +
            "SELECT * FROM ums_resource WHERE id in " +
            "(SELECT resource_id from ums_role_resource_relation WHERE role_id in " +
            "<foreach item = 'item' index = 'index' collection = 'ids' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "GROUP BY resource_id" +
            ")" +
            "</script>")
    List<UmsResource> selectUmsResource(@Param("ids") List<Long> ids);

    /***
     * 给角色添加关联的资源
     * @author Cr Mr
     * @date 2022/4/3 13:03
     */
    @Insert("<script>" +
            "INSERT INTO ums_role_resource_relation (role_id,resource_id) values" +
            "<foreach collection=\"items\" index=\"index\" item=\"item\" separator=\",\"> " +
            "(#{roleId},#{item})" +
            "</foreach> " +
            "</script>")
    void allocResource(@Param("roleId") Long roleId, @Param("items") List<Long> items);
}
